using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace Send2Sharepoint.Forms
{
    public class ChoiceControl:InputControl
    {
        public ChoiceControl(XmlNode element) : base(element)
        {
            var attributeCollection = element.Attributes;
            var name = attributeCollection["Name"].Value;
            var format = attributeCollection["Format"].Value;

            var choicesNode = (from xmlNode in element.ChildNodes.Cast<XmlNode>()
                               where xmlNode.Name == "CHOICES"
                               select xmlNode).SingleOrDefault();
            var defaultNode = (from xmlNode in element.ChildNodes.Cast<XmlNode>()
                               where xmlNode.Name == "Default"
                               select xmlNode).SingleOrDefault();

            var defaultValue = defaultNode != null ? defaultNode.InnerText : "";
            var choices = from choice in choicesNode.ChildNodes.Cast<XmlNode>()
                      select choice.InnerText;
            const int x = 10;
            var y = 10;
            switch (format)
            {
                case "RadioButtons":
                    Field = new GroupBox
                    {
                        Name = "controlGroup_Radio",
                        Size = new Size(250, 22)
                    };
                    ((GroupBox)Field).FlatStyle = FlatStyle.System;
                    foreach (var choice in choices)
                    {
                        var radio = new RadioButton
                        {
                            Location = new Point(x, y),
                            Text = choice,
                            Name = name + "_" + choice
                        };
                        Field.Controls.Add(radio);
                        radio.AutoSize = true;
                        y += radio.Size.Height;
                        Field.Height = y + 5;
                        if (choice == defaultValue) radio.Checked = true;
                    }
                    break;
                case "Dropdown":
                    Field = new ComboBox
                    {
                        Name = "controlCombo_" + name,
                        Size = new Size(250, 22)
                    };
                    if (attributeCollection["FillInChoice"] != null && attributeCollection["FillInChoice"].Value == "TRUE")
                        ((ComboBox)Field).DropDownStyle = ComboBoxStyle.DropDown;
                    else
                    {
                        ((ComboBox)Field).DropDownStyle = ComboBoxStyle.DropDownList;
                        ((ComboBox)Field).Items.Add("");
                    }
                    foreach (var choice in choices)
                    {
                        ((ComboBox)Field).Items.Add(choice);
                    }
                    ((ComboBox)Field).SelectedIndex = ((ComboBox)Field).FindString(defaultValue);
                    break;
            }
        }
    }
}